<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags" %>
<%
String basePath = request.getContextPath();
%>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>预约管理</title>
		<link rel="stylesheet" href="<%=basePath %>/css/style.css" type="text/css" />
		<script type="text/javascript" src="<%=basePath %>/js/My97DatePicker/WdatePicker.js"></script>
		<script type='text/javascript' src='<%=basePath %>/js/jquery1.3.2/js/jquery-1.3.2.min.js'></script>
		<script type='text/javascript' src='<%=basePath %>/js/jquery1.3.2/js/jquery.form.js'></script>
		<script src="<%=basePath %>/js/util/querycombo.js" type="text/javascript"></script>
		<script laguage="javascript">var baseUrl = "<%=basePath %>";</script>
	</head>

	<body onload="window.focus()">
		<div id="rollbar">
			<div>
				<label accesskey="e"></label><s:actionerror/>
				<div class="tabcontainer">
					<form id="form" name="form" method="post"
						action="<%=basePath %>/order/saveReservation.do">
						<input type="hidden" name="orderId" value="<s:property value="orderInfo.id"/>"/>
						<input type="hidden" id="groupOrderId" name="groupOrderId" value="<s:property value="orderInfo.groupOrderId"/>"/>
						<s:token name="sendMessageToken"/>
						<table id="edittable" style="width:900px;">
							<tr>
								<td colspan="4" class="column_head" style="font-size:14px;font-weight:bold;">
									订单信息
								</td>
							</tr>
							<tr>
								<td class="column_name" style="width:15%">
									订单号
								</td>
								<td class="column_input" style="width:35%">
									<s:property value="orderInfo.orderNo"/>
								</td>
								<td class="column_name" style="width:15%">
									订单来源
								</td>
								<td class="column_input" style="width:35%">
									<s:property value="orderInfo.orderFrom"/>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									购买人
								</td>
								<td class="column_input">
									<s:property value="orderInfo.buyUser"/>
								</td>
								<td class="column_name">
									有效期
								</td>
								<td class="column_input">
									<s:date name="orderInfo.startDate" format="yyyy-MM-dd"/>至<s:date name="orderInfo.endDate" format="yyyy-MM-dd"/>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									邮寄地址
								</td>
								<td class="column_input">
									<s:property value="orderInfo.address"/>
								</td>
								<td class="column_name">
									邮政编码
								</td>
								<td class="column_input" >
									<s:property value="orderInfo.postcode"/>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									<b>体检人姓名</b>
								</td>
								<td class="column_input">
									<input type="text" name="reservation.tijian" id="tijian"
										value="<s:property value="orderInfo.tijian"/>" size="25" maxlength="20" />
								</td>
								<td class="column_name">
									<b>性别</b>
								</td>
								<td class="column_input">
									<input type="radio" name="reservation.sex" value="男" <s:if test="orderInfo.sex == null || orderInfo.sex ==\"男\"">checked="checked"</s:if>>男
									<input type="radio" name="reservation.sex" value="女" <s:if test="orderInfo.sex ==\"女\"">checked="checked"</s:if>>女
								</td>
							</tr>
							<tr>
								<td class="column_name">
									<b>年龄</b>
								</td>
								<td class="column_input">
									<input type="text" name="reservation.age" id="age"
										value="<s:property value="orderInfo.age"/>" size="25" maxlength="3"/>
								</td>
								<td class="column_name">
									<b>身份证号</b>
								</td>
								<td class="column_input">
									<input type="text" name="reservation.idcard"
										value="<s:if test="orderInfo.idcard!=null"><s:property value="orderInfo.idcard"/></s:if>" size="25" maxlength="30"/>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									电话号码
								</td>
								<td class="column_input">
									<input type="text" name="reservation.phone" id="phone"
										value="<s:if test="orderInfo.phone!=null"><s:property value="orderInfo.phone"/></s:if>" size="25" maxlength="40"/>
									
								</td>
								<td class="column_name">
									手机
								</td>
								<td class="column_input">
									<input type="text" name="reservation.mobile" id="mobile"
										value="<s:if test="orderInfo.mobile!=null"><s:property value="orderInfo.mobile"/></s:if>" size="25" maxlength="40"/>
								</td>
							</tr>
						</table>
						<table id="edittable" style="width:900px;">
							<tr>
								<td colspan="4" class="column_head" style="font-size:14px;font-weight:bold;">
									预约信息
								</td>
							</tr>
							<tr>
								<td class="column_name" style="width:15%">
									合作机构
								</td>
								<td class="column_input" style="width:35%">
									<s:select id="institutionId" onchange="getProduct();getBranch()" list="institutionList" listKey="id" listValue="name" headerKey="-1" headerValue="请选择合作商家" name="institutionId" cssStyle="width:300px;"></s:select>
									<s:if test="orderInfo.institution == null ||orderInfo.product == null">
									<script language="javascript">showQueryBox("institutionId", "getProduct();getBranch()");</script>
									</s:if>
								</td>
								<td class="column_name" style="width:15%">
									套餐名称
								</td>
								<td class="column_input"style="width:35%">
									<span style="float:left;clear:both;"  id="sproduct" ><s:select id="productId" onchange="getPrice()" list="productList" listKey="id" listValue="name" headerKey="-1" headerValue="请选择" name="productId" cssStyle="width:300px"></s:select></span>
									<s:if test="orderInfo.institution == null || orderInfo.productInfo == null">
									<span style="float:left;"><script language="javascript">showQueryBox("productId", "getPrice()");</script></span>
									</s:if>
								</td>
							</tr>
							<tr>
								<td class="column_name" style="width:15%">
									&nbsp;
								</td>
								<td class="column_input" style="width:35%">
									<s:if test="orderInfo.institutionName != null">
										<s:property value="orderInfo.institutionName"/>
									</s:if>
									
									<s:elseif test="orderInfo.institution != null">
										<s:property value="orderInfo.institution.name"/>
									</s:elseif>
								</td>
								<td class="column_name">
									&nbsp;
								</td>
								<td class="column_input" >
									<s:if test="orderInfo.productName != null">
										<s:property value="orderInfo.productName"/>
									</s:if>
									<s:elseif test="orderInfo.product != null">
										<s:property value="orderInfo.product.name"/>
									</s:elseif>
									<input type="hidden" id="defaultProductName" value="<s:if test="orderInfo.productName != null"><s:property value="orderInfo.productName"/></s:if><s:elseif test="orderInfo.product != null"><s:property value="orderInfo.product.name"/></s:elseif>"/>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									成交价格
								</td>
								<td class="column_input">
									<input type="text" name="reservation.salePrice" readonly="readonly"
										value="<s:if test="orderInfo.salePrice != null"><s:property value="orderInfo.salePrice"/></s:if><s:else>0</s:else>" size="25" />
								</td>
								<td class="column_name">
									销售价格
								</td>
								<td class="column_input" id="productprice">
									<s:if test="orderInfo.product != null">
										<s:property value="orderInfo.product.price"/>
									</s:if>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									预约日期
								</td>
								<td class="column_input">
									<input type="text" id="reserveDate" name="reservation.reserveDate"
										value=""
										size="25" dataType="Require" msg="请输入预约日期" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',readOnly:true})" />
									<input type="button" value="营业时间" onclick="showWorkTime()" style="width:60px;"/>
								</td>
								<td class="column_name">
									预约分院
								</td>
								<td class="column_input" id="branchContainer">
									<s:if test="institutionBranchList != null && institutionBranchList.size()>0">
										<s:select name="reservation.branch" id="branch" list="institutionBranchList" listKey="name" listValue="name" headerKey="-1" headerValue="请选择分院"></s:select>
									</s:if>
									<s:else>
									<input type="text" name="reservation.branch" id="branch"
										value="" size="25" maxlength="20"/>
									</s:else>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									预约备注
								</td>
								<td class="column_input" colspan="3">
									<input type="text" name="reservation.comments"
										value="<s:property value="reservation.comments"/>" size="86" maxlength="100"/>
								</td>
							</tr>
							<tr>
								<td class="column_name">
									录单备注
								</td>
								<td class="column_input" colspan="3">
									<input type="text" name="orderInfo.comment"
										value="<s:property value="orderInfo.comment"/>" size="86" maxlength="100"/>
								</td>
							</tr>
							
							<tr>
								<td colspan="4" class="column_bottom">
									<input name="submit" id="submitbut" type="button" value="提交" onclick="saveReservation()"/>
									<input name="reset" type="reset" id="reset" value="重置" />
								</td>
							</tr>
						</table>
					</form>
				</div>

			</div>
		</div>
		
		<form action="" id="wtForm" method="post" target="_blank"></form>
		<script language="javascript">
			function getPrice(){
				jQuery("#productprice").html("");
				jQuery("#productprice").html(jQuery("#productId").find("option[selected=true]").attr("price"));
			}
			
			function getProduct(){
				document.getElementById("productprice").innerHTML = "";
				var url = "<%=basePath %>/order/getJSONProductInfo.do";
				var groupOrderId = document.getElementById("groupOrderId").value;
				var institution=document.getElementById('institutionId').value;
				var p = jQuery("#productId");
				p.css("width","300px");
				p.attr("disabled", false);
				if(institution == "-1"){
					return;
				}
				document.getElementById("productId").options.length=1;
				jQuery.post(url,{institutionId:institution,groupOrderId:groupOrderId},function(data){
					for(var i=0;i<data.length;i++){
						p.append("<option value='" + data[i][0] + "' price='" + data[i][2] + "'>" + data[i][1] + "</option>");
					}
					var defaultProductName = jQuery("#defaultProductName").val();
    				if(defaultProductName != ""){
    					var opts = jQuery("#productId").find("option[text='" + defaultProductName + "']");
    					if(opts.length>0){
    						$(opts[0]).attr("selected", true);
    						jQuery("#productId").attr("disabled", true);
    						getPrice();
    					}
    				}
				}, "json");
			}
			function getBranch(){
				var c = $("#branchContainer");
				c.html("");
				var url = "<%=basePath %>/order/getJSONInstitutionBranch.do";
				var institution=document.getElementById('institutionId').value;
				if(institution == "-1"){
					return;
				}
				jQuery.post(url,{Q_institutionId_L_EQ:institution},function(data){
					if(data.length>0){
						html = [];
						html.push('<select name="reservation.branch" id="branch">');
						for(var i=0;i<data.length;i++){
							html.push("<option value='" + data[i][1] + "'>" + data[i][1] + "</option>");
						}
						html.push("</select>");
						c.html(html.join(""));
					}else{
						c.html('<input type="text" name="reservation.branch" id="branch" value="" size="25" maxlength="20"/>');
					}
				}, "json");
			}	
    		function checkForm(){
    			try{
	    			
	    			var tj = document.getElementById("tijian");
	    			if(tj.value==""){
	    				tj.focus();
	    				alert("请输入体检人姓名");
	    				return false;
	    			}
	    			
	    			var sex=document.getElementsByName("reservation.sex");
					var sexStr;
					if(sex[0].checked==false && sex[1].checked==false)
					{
						alert("请选择性别!");
						return false;
					}else{
						sexStr = (sex[0].checked)?sex[0].value:sex[1].value;
					}
					var age = document.getElementById("age");
	    			if(age.value==""){
	    				age.focus();
	    				alert("请输入体检人年龄");
	    				return false;
	    			}
	    			
	    			var inst = document.getElementById("institutionId");
	    			if(inst && inst.value == -1){
	    				inst.focus();
	    				alert("没有选择合作商家");
	    				return false;
	    			}
	    			var pro = document.getElementById("productId");
	    			if(pro && pro.value == -1){
	    				pro.focus();
	    				alert("没有选择产品");
	    				return false;
	    			}
	    			var pname = pro.options[pro.selectedIndex].text;
					if(("男" == sexStr && pname.indexOf("女") != -1)||("女" == sexStr && pname.indexOf("男") != -1)){
						alert("所选套餐对应的性别不正确！");
						return false;
					}

					var rd = document.getElementById("reserveDate");
	    			if(rd.value==""){
	    				rd.focus();
	    				alert("请选择预约日期");
	    				return false;
	    			}
	    			var phone = document.getElementById("phone");
					var mobile= document.getElementById("mobile");
	    			if(phone.value=="" && mobile.value==""){
	    				if(!confirm("没有输入联系方式，确认继续预约吗？")){
	    					return false;
	    				}
	    			}
					document.getElementById("submitbut").disabled=true;
					jQuery("#institutionId").attr("disabled", false);
					jQuery("#productId").attr("disabled", false);
					return true;
				}catch(e){
					return false;
				}
    		}
    		function saveReservation(){
    			if(checkForm()){
    				var options = {
						success:function(data){
							if(data.success){
								alert("预约成功！");
								try{
									window.parent.OrderInfoView.reloadData();
								}catch(e){};
								try{
									window.parent.ReservationView.reloadData();
								}catch(e){};
							}
						},
						dataType:"json"
					}; 
					$('#form').ajaxForm(options).submit();
    			}
    		}
    		jQuery(document).ready(function(){
    			<s:if test="orderInfo.institution != null && orderInfo.institution.name.indexOf(\"通用\") == -1 && orderInfo.product != null">
    				jQuery("#institutionId").attr("disabled", true);
    				jQuery("#productId").attr("disabled", true);
    			</s:if>
    		});
		</script>
	</body>
</html>
